package cn.tt.tuantuanerp.tuantuanerp.warehouse.service.impl;


import cn.tt.tuantuanerp.tuantuanerp.comment.ex.ServiceException;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.dto.ProductCheckDTO;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.dto.WarehouseCheckDTO;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.entity.ProductCheck;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.entity.SkuCheckInfo;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.entity.WarehouseCheck;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.vo.ProductCheckVO;
import cn.tt.tuantuanerp.tuantuanerp.comment.security.LoginPrincipal;
import cn.tt.tuantuanerp.tuantuanerp.comment.web.ServiceCode;
import cn.tt.tuantuanerp.tuantuanerp.product.service.ISkuService;
import cn.tt.tuantuanerp.tuantuanerp.warehouse.mapper.ProductCheckMapper;
import cn.tt.tuantuanerp.tuantuanerp.warehouse.mapper.WarehouseCheckMapper;
import cn.tt.tuantuanerp.tuantuanerp.warehouse.service.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    ProductCheckMapper productMapper;

    @Autowired
    WarehouseCheckMapper warehouseCheckMapper;
    @DubboReference
    ISkuService skuService;

    @Override
    public void addNew(WarehouseCheckDTO warehouseCheckDTO, LoginPrincipal loginPrincipal) {
        Long checkWarehouseId = warehouseCheckDTO.getCheckWarehouseId();
        String username = loginPrincipal.getUsername();
        log.debug("Service:开始处理添加盘点批次和商品列表的业务参数:{},处理人:{}",warehouseCheckDTO,username);
        WarehouseCheck warehouseCheck = new WarehouseCheck();
        BeanUtils.copyProperties(warehouseCheckDTO,warehouseCheck);
        warehouseCheck.setCheckWarehouseId(warehouseCheckDTO.getCheckWarehouseId());
        warehouseCheck.setApprovalStatus((short)0);
        warehouseCheck.setCheckPerson(username);
        if (warehouseCheck.getCheckPerson()==null){
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"请登录");
        }
        warehouseCheck.setCheckTime(LocalDateTime.now());
        int insert = warehouseCheckMapper.insert(warehouseCheck);
        if (insert==0){
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"盘点批次录入异常");
        }
        log.debug("开始处理添加盘点商品事件{}",warehouseCheckDTO.getArrayData());
        List<ProductCheck> list = new ArrayList<>();
        List<ProductCheckDTO> productCheckDTOList = warehouseCheckDTO.getArrayData();
        SkuCheckInfo[] skuCheckInfos = new SkuCheckInfo[productCheckDTOList.size()-1];
        for (int i = 0; i < productCheckDTOList.size()-1; i++) {
            ProductCheck productCheck = new ProductCheck();
            productCheckDTOList.get(i).setCheckWarehouseId(checkWarehouseId);
            BeanUtils.copyProperties(productCheckDTOList.get(i),productCheck);
            productCheck.setCheckNumberResult(productCheck.getCheckNumber()-productCheck.getExpectNumber());
            list.add(productCheck);

            SkuCheckInfo skuCheckInfo = new SkuCheckInfo();
            BeanUtils.copyProperties(productCheckDTOList.get(i),skuCheckInfo);
            skuCheckInfo.setCheckNumberResult(skuCheckInfo.getCheckNumber()-skuCheckInfo.getExpectNumber());
            skuCheckInfos[i]=skuCheckInfo;
        }
        log.debug("开始处理添加盘点商品列表{}",list);
        productMapper.insertBatch(list);
        log.debug("开始处理更改sku商品列表{}",skuCheckInfos);
        skuService.setCheckInfoBat(warehouseCheckDTO.getCheckWarehouseId(),skuCheckInfos, loginPrincipal);
    }

    @Override
    public List<ProductCheckVO> list(Long id) {
        log.debug("开始获取盘点批次为:{}的商品列表",id);
        List<ProductCheckVO> productCheckVOS = productMapper.listByCheckId(id);
        return productCheckVOS;
    }

}
